package com.lottery.service.pc28.statisticsService.impl;

import com.lottery.dao.pc28.accoutDao.UserDao;
import com.lottery.dao.pc28.statisticsDao.UserTop10Dao;
import com.lottery.dto.ResultDto;
import com.lottery.entity.UserTop10;
import com.lottery.service.pc28.statisticsService.UserTop10Service;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.List;

@Service
public class UserTop10ServiceImpl implements UserTop10Service {

	@Resource
	private UserTop10Dao userTop10Dao;

	@Resource
	private UserDao userDao;

	/**
	 * 插入数据
	 * 
	 * @param expect
	 *            期号
	 * @return
	 */
	@Override
	public ResultDto<UserTop10> insetUserTop10(String expect, int lotteyType) {
		ResultDto<UserTop10> resultDto = new ResultDto<UserTop10>();
		UserTop10 userTop10 = new UserTop10();
		userTop10.setExpect(expect);
		userTop10.setLotteyType(lotteyType);
		List<UserTop10> list = userTop10Dao.getUsreTop10Limit(userTop10);
		UserTop10 ut10 = new UserTop10();
		if (list != null && list.size() > 0) {
			for (UserTop10 userTop10limit : list) {
				userTop10 = new UserTop10();
				// 用户id
				int userId = userTop10limit.getUserId();
				String username = userDao.selectId(userId).getUserName();
				// 中奖总金额
				double userWinTotalMoney = userTop10limit.getUserWinTotalMoney();
				// 投注总金额
				double userBetTotalMoney = userTop10limit.getUserBetTotalMoney();
				double income = 0;
				ut10.setExpect(expect);
				ut10.setUserId(userId);
				ut10.setLotteyType(lotteyType);
				double bettingmoney = userTop10Dao.getBettingmoney(ut10);
				double winningamount = userTop10Dao.getWinningamount(ut10);
				income = winningamount - bettingmoney;
				DecimalFormat df = new DecimalFormat("#0.##");
				df.setRoundingMode(RoundingMode.FLOOR);
				income = Double.valueOf(df.format(income));
				userTop10.setExpect(expect);
				userTop10.setUserBetTotalMoney(Double.parseDouble(df.format(userBetTotalMoney)));
				userTop10.setUserId(userId);
				userTop10.setUsername(username);
				userTop10.setUserWinTotalMoney(Double.parseDouble(df.format(userWinTotalMoney)));
				userTop10.setIncome(income);
				userTop10.setLotteyType(lotteyType);
				try {
					// 执行插入操作
					userTop10Dao.insertUserTop10(userTop10);
					resultDto.setMessage("200");
					resultDto.setSuccess(true);
				} catch (Exception e) {
					e.printStackTrace();
					resultDto.setMessage("400" + e.toString());
					resultDto.setSuccess(false);
					return resultDto;
				}

			}
		}
		return resultDto;
	}

}
